using System.Text.RegularExpressions;

namespace WIT.DAQ.Core.Extension;

/// <summary>
/// String扩展类
/// </summary>
internal static class StringExtension
{
	/// <summary>
	/// 判断是否为十六进制格式字符串
	/// </summary>
	/// <param name="str"></param>
	/// <returns></returns>
	internal static bool IsHex(this string str)
	{
		bool flag = Regex.IsMatch(str, "[A-Fa-f0-9]+$");
		if (flag)
		{
			if ((str.Length + 1) % 3 != 0)
			{
				flag = false;
			}
			else if (str.Length > 2)
			{
				string[] array = str.Split(' ');
				int num = array.Length - 1;
				int num2 = (str.Length + 1) / 3 - 1;
				int count = Regex.Matches(str, " ").Count;
				if (num != num2 || count != num)
				{
					flag = false;
				}
				else
				{
					for (int i = 0; i < str.Length; i++)
					{
						if ((i + 1) % 3 == 0 && str[i] != ' ')
						{
							flag = false;
							break;
						}
					}
				}
			}
			else if (str.Length < 2)
			{
				flag = false;
			}
		}
		return flag;
	}
}
